package com.example.serialportdemo;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    public static  String getCurrentTimeYMD(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date());
    }

    public static  String getCurrentTimeYMDHMS(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }

    public static String getCurrentTimeYMDHMS2(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(new Date());
    }

    /**
     * yyyy-MM-dd HH:mm:ss
     * @param longDate
     * @return
     */
    public static String formatDateTimeYMDHMS(long longDate){
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(longDate));
    }

    /**
     * 转为 yyyy-MM-dd
     * @param longDate
     * @return
     */
    public static String formatDateTimeYMD(long longDate){
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date(longDate));
    }

    /**
     * 获取某天 第一秒的  时间戳
     * @param calendar
     * @return
     */
    public static long getDayFirstSecond(Calendar calendar){
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取某天的最后一秒 的时间戳
     * @param calendar
     * @return
     */
    public static long getDayLastSecond(Calendar calendar){
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取过去一周前 的此时时间戳
     * @return
     */
    public static long getFirstWeekFirstSecond(){
        Calendar stCal = Calendar.getInstance();
        //过去七天
        stCal.setTime(new Date());
        stCal.add(Calendar.DATE, - 7);
        return stCal.getTimeInMillis();
    }


}
